<div class="problemindexholder" problemindex="B">
 <div class="ttypography">
  <div class="problem-statement">
   <div class="header">
    <div class="title">
     B. Cubes for Masha
    </div>
    <div class="time-limit">
     <div class="property-title">
      time limit per test
     </div>
     1 second
    </div>
    <div class="memory-limit">
     <div class="property-title">
      memory limit per test
     </div>
     256 megabytes
    </div>
    <div class="input-file">
     <div class="property-title">
      input
     </div>
     standard input
    </div>
    <div class="output-file">
     <div class="property-title">
      output
     </div>
     standard output
    </div>
   </div>
   <div>
    <p>
     Absent-minded Masha got set of
     <span class="tex-span">
      <i>
       n
      </i>
     </span>
     cubes for her birthday.
    </p>
    <p>
     At each of 6 faces of each cube, there is exactly one digit from 0 to 9. Masha became interested what is the largest natural
     <span class="tex-span">
      <i>
       x
      </i>
     </span>
     such she can make using her new cubes all integers from 1 to
     <span class="tex-span">
      <i>
       x
      </i>
     </span>
     .
    </p>
    <p>
     To make a number Masha can rotate her cubes and put them in a row. After that, she looks at upper faces of cubes from left to right and reads the number.
    </p>
    <p>
     The number can't contain leading zeros. It's not required to use all cubes to build a number.
    </p>
    <p>
     Pay attention: Masha can't make digit 6 from digit 9 and vice-versa using cube rotations.
    </p>
   </div>
   <div class="input-specification">
    <div class="section-title">
     Input
    </div>
    <p>
     In first line integer
     <span class="tex-span">
      <i>
       n
      </i>
     </span>
     is given (
     <span class="tex-span">
      1 ≤
      <i>
       n
      </i>
      ≤ 3
     </span>
     ) — the number of cubes, Masha got for her birthday.
    </p>
    <p>
     Each of next
     <span class="tex-span">
      <i>
       n
      </i>
     </span>
     lines contains 6 integers
     <span class="tex-span">
      <i>
       a
      </i>
      <sub class="lower-index">
       <i>
        i
       </i>
      </sub>
      <sub class="lower-index">
       <i>
        j
       </i>
      </sub>
     </span>
     (
     <span class="tex-span">
      0 ≤
      <i>
       a
      </i>
      <sub class="lower-index">
       <i>
        i
       </i>
      </sub>
      <sub class="lower-index">
       <i>
        j
       </i>
      </sub>
      ≤ 9
     </span>
     ) — number on
     <span class="tex-span">
      <i>
       j
      </i>
     </span>
     -th face of
     <span class="tex-span">
      <i>
       i
      </i>
     </span>
     -th cube.
    </p>
   </div>
   <div class="output-specification">
    <div class="section-title">
     Output
    </div>
    <p>
     Print single integer — maximum number
     <span class="tex-span">
      <i>
       x
      </i>
     </span>
     such Masha can make any integers from 1 to
     <span class="tex-span">
      <i>
       x
      </i>
     </span>
     using her cubes or 0 if Masha can't make even 1.
    </p>
   </div>
   <div class="sample-tests">
    <div class="section-title">
     Examples
    </div>
    <div class="sample-test">
     <div class="input">
      <div class="title">
       Input
      </div>
      <pre>3<br/>0 1 2 3 4 5<br/>6 7 8 9 0 1<br/>2 3 4 5 6 7<br/></pre>
     </div>
     <div class="output">
      <div class="title">
       Output
      </div>
      <pre>87</pre>
     </div>
     <div class="input">
      <div class="title">
       Input
      </div>
      <pre>3<br/>0 1 3 5 6 8<br/>1 2 4 5 7 8<br/>2 3 4 6 7 9<br/></pre>
     </div>
     <div class="output">
      <div class="title">
       Output
      </div>
      <pre>98</pre>
     </div>
    </div>
   </div>
   <div class="note">
    <div class="section-title">
     Note
    </div>
    <p>
     In the first test case, Masha can build all numbers from 1 to 87, but she can't make 88 because there are no two cubes with digit 8.
    </p>
   </div>
  </div>
  <p>
  </p>
 </div>
</div>
